package com.chamberlain.equipment.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSON;
import com.chamberlain.common.constant.HttpStatus;
import com.chamberlain.common.core.domain.entity.Equipment;
import com.chamberlain.common.core.domain.entity.Scene;
import com.chamberlain.common.exception.ServiceException;
import com.chamberlain.common.utils.DateUtils;
import com.chamberlain.equipment.domain.SceneEquipment;
import com.chamberlain.equipment.mapper.SceneEquipmentMapper;
import com.chamberlain.equipment.mapper.SceneMapper;
import com.chamberlain.equipment.mqtt.MqttPublishClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import com.chamberlain.equipment.mapper.EquipmentMapper;
import com.chamberlain.equipment.service.IEquipmentService;

import javax.annotation.Resource;

/**
 * 设备详情Service业务层处理
 * 
 * @author chamberlain
 * @date 2024-03-23
 */
@Service
public class EquipmentServiceImpl implements IEquipmentService 
{
    @Resource
    private EquipmentMapper equipmentMapper;

    @Resource
    private SceneEquipmentMapper sceneEquipmentMapper;
    @Resource
    private MqttPublishClient mqttPublishClient;


    @Resource
    private SceneMapper sceneMapper;
    /**
     * 查询设备
     *
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public Equipment selectEquipmentById(Long id)
    {
        return equipmentMapper.selectEquipmentById(id);
    }

    /**
     * 查询设备列表
     *
     * @param equipment 设备
     * @return 设备
     */
    @Override
    public List<Equipment> selectEquipmentList(Equipment equipment)
    {
        return equipmentMapper.selectEquipmentList(equipment);
    }

    /**
     * 新增设备
     *
     * @param equipment 设备
     * @return 结果
     */
    @Override
    public int insertEquipment(Equipment equipment)
    {
        try {
            equipment.setCreatedTime(DateUtils.getNowDate());
            equipment.setTopic("chamberlain/"+equipment.getMac()+"/disjunction");
            equipmentMapper.insertEquipment(equipment);
        }catch (DataIntegrityViolationException e){
            throw new ServiceException("请检查表单，不允许有重复的名称或mac", HttpStatus.ERROR);
        }

        Integer equipmentId = equipmentMapper.selectEquipmentByName(equipment.getName());
        SceneEquipment sceneEquipment=new SceneEquipment();
        Integer sceneId = sceneMapper.selectSceneByName(equipment.getSceneName());
        sceneEquipment.setSceneId(sceneId);
        sceneEquipment.setEquipmentId(equipmentId);
        return sceneEquipmentMapper.insert(sceneEquipment);
    }

    /**
     * 修改设备
     *
     * @param equipment 设备
     * @return 结果
     */
    @Override
    public int updateEquipment(Equipment equipment)
    {
        equipment.setUpdateTime(DateUtils.getNowDate());
        equipment.setTopic("chamberlain/"+equipment.getMac()+"/disjunction");

        SceneEquipment sceneEquipment=new SceneEquipment();
        Integer sceneId = sceneMapper.selectSceneByName(equipment.getSceneName());
        sceneEquipment.setId(sceneEquipmentMapper.queryByEquipmentId(equipment.getId()).intValue());
        sceneEquipment.setSceneId(sceneId);
        sceneEquipment.setEquipmentId(equipment.getId().intValue());
        sceneEquipmentMapper.update(sceneEquipment);
        return equipmentMapper.updateEquipment(equipment);
    }

    /**
     * 批量删除设备
     *
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentByIds(Long[] ids)
    {
        Long[] sceneIds = sceneEquipmentMapper.queryByEquipmentIds(ids);
        for (int i = 0; i < sceneIds.length; i++) {
            sceneEquipmentMapper.deleteById(sceneIds[i].intValue());
        }
        return equipmentMapper.deleteEquipmentByIds(ids);
    }

    /**
     * 删除设备信息
     *
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentById(Long id)
    {
        return equipmentMapper.deleteEquipmentById(id);
    }

    /**
     * 根据sceneId查询对应场景下的所有设备
     * @param scene
     * @return 设备List
     */
    @Override
    public List<Equipment> selectEquipmentListBySceneId(Scene scene) {
        List<Equipment> equipmentList =new ArrayList<>();
        Long[] equipmentIds = sceneEquipmentMapper.queryAllBySceneId(scene.getId());
        for (int i = 0; i < equipmentIds.length; i++) {
            equipmentList.add(equipmentMapper.selectEquipmentById(equipmentIds[i]));
        }
        return equipmentList;
    }

    @Override
    public void updateEquipmentByMac(Equipment equipment) {
        equipment.setUpdateTime(DateUtils.getNowDate());

        Map<String,Object> map=new HashMap<>();
        map.put("mac",equipment.getMac());
        map.put("disjunction",equipment.getDisjunction());
        String json = JSON.toJSONString(map);
        mqttPublishClient.publish(2,false,"chamberlain/"+equipment.getMac()+"/disjunction",json);
        equipmentMapper.updateEquipmentByMac(equipment);
    }
}
